/*
 * The UserServiceBean.java created for spikeCASservice projects. 
 */
package com.google.code.sip.ucenter.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.code.sip.core.dao.Page;
import com.google.code.sip.ucenter.dao.CreditDao;
import com.google.code.sip.ucenter.dao.CreditRuleDao;
import com.google.code.sip.ucenter.dao.SettingDao;
import com.google.code.sip.ucenter.dao.mongo.CreditTypeDaoImpl;
import com.google.code.sip.ucenter.model.Credit;
import com.google.code.sip.ucenter.model.CreditRule;
import com.google.code.sip.ucenter.model.CreditType;
import com.google.code.sip.ucenter.model.Setting;
import com.google.code.sip.ucenter.service.CreditTypeManager;

/**
 * 
 * @author Steven Liu
 * @version 1.0 2010-5-31
 */
@Service("creditTypeManager")
public class CreditTypeManagerImpl implements CreditTypeManager {
	@Autowired
	private CreditTypeDaoImpl creditTypeDao;

	@Autowired
	private SettingDao settingDao;

	@Autowired
	private CreditDao creditDao;

	@Autowired
	private CreditRuleDao creditRuleDao;

	@Override
	public CreditType getCreditTypeByName(String creditTypeName) {
		return creditTypeDao.getByName(creditTypeName);
	}

	@Override
	public void deleteCreditType(String creditTypeId) {
		creditTypeDao.delete(creditTypeId);

	}

	@Override
	public Page<CreditType> getAllCreditTypes(Page<CreditType> page) {
		return creditTypeDao.findByPage(page);
	}

	@Override
	public List<CreditType> getAllCreditTypes() {
		return creditTypeDao.findAll();
	}

	@Override
	public CreditType getCreditType(String creditTypeId) {
		return creditTypeDao.get(creditTypeId);
	}

	@Override
	public void saveCreditType(CreditType creditType) {
		creditTypeDao.save(creditType);
	}

	@Override
	public List<String> getAllCreditTypeNames() {
		List<CreditType> creditTypes = getAllCreditTypes();
		if (creditTypes == null || creditTypes.isEmpty()) {
			return null;
		}
		List<String> creditTypeNames = new ArrayList<String>(creditTypes.size());
		for (CreditType creditType : creditTypes) {
			creditTypeNames.add(creditType.getName());
		}
		return creditTypeNames;
	}

	@Override
	public boolean isTransCreditType(String creditTypeId) {
		CreditType creditType = this.getCreditType(creditTypeId);
		if (creditType == null) {
			return false;
		}
		return isTransCreditType(creditType);
	}

	@Override
	public boolean isTransCreditType(CreditType creditType) {
		Setting transCreditSetting = settingDao
				.getByName(Setting.CREDIT_TRANSFER);
		if (transCreditSetting == null) {
			return false;
		}
		return creditType.getName().equals(transCreditSetting.getValue());
	}

	@Override
	public boolean isCreditTypeUsed(String id) {
		CreditType creditType = this.getCreditType(id);
		if (creditType == null) {
			return false;
		}
		return isCreditTypeUsed(creditType);
	}

	@Override
	public boolean isCreditTypeUsed(CreditType creditType) {
		List<Credit> credits = creditDao.findByCreditType(creditType);
		if (credits != null && !credits.isEmpty()) {
			return true;
		}
		List<CreditRule> creditRules = creditRuleDao
				.findByCreditType(creditType);
		if (creditRules != null && !creditRules.isEmpty()) {
			return true;
		}
		return false;
	}
}
